package counsel;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.PagingVo;
import contract.ContractDTO;
import code.CodeDAO;
import code.CodeDTO;

public class CounselList extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		req.setCharacterEncoding("utf-8");
		
		String full = req.getParameter("full");
		String c_full = (String)req.getSession().getAttribute("c_full");
		
		CounselDTO coun = new CounselDTO();
		CounselDAO dao = new CounselDAO();
		
		//검색 했던적있으면 여기에 걸림
		if((full != null && full.equals("full")) || c_full != null){
			coun.setFull("full");
			dao.setCoun(coun);
			
			if(full != null && full.equals("full")){//검색버튼 눌렀을시
				req.getSession().setAttribute("c_full", "full");
				coun.setSa_search(req.getParameter("sa_search"));
				req.getSession().setAttribute("c_sa_search", coun.getSa_search());
				coun.setPd_search(req.getParameter("pd_search"));
				req.getSession().setAttribute("c_pd_search", coun.getPd_search());
				coun.setCs_search(req.getParameter("cs_search"));
				req.getSession().setAttribute("c_cs_search", coun.getCs_search());
				coun.setSa_dpm(req.getParameter("sa_dpm"));
				req.getSession().setAttribute("c_sa_dpm", coun.getSa_dpm());
				coun.setSa_gd(req.getParameter("sa_gd"));
				req.getSession().setAttribute("c_sa_gd", coun.getSa_gd());
				coun.setCs_loan(req.getParameter("cs_loan"));
				req.getSession().setAttribute("c_cs_loan", coun.getCs_loan());
				coun.setCt_ing(req.getParameter("ct_ing"));
				req.getSession().setAttribute("c_ct_ing", coun.getCt_ing());
				
			}else if(c_full != null && c_full.equals("full")){//페이징이나 검색후 목록으로 왔을시
				coun.setSa_search((String)req.getSession().getAttribute("c_sa_search"));
				coun.setPd_search((String)req.getSession().getAttribute("c_pd_search"));
				coun.setCs_search((String)req.getSession().getAttribute("c_cs_search"));
				coun.setSa_dpm((String)req.getSession().getAttribute("c_sa_dpm"));
				coun.setSa_gd((String)req.getSession().getAttribute("c_sa_gd"));
				coun.setCs_loan((String)req.getSession().getAttribute("c_cs_loan"));
				coun.setCt_ing((String)req.getSession().getAttribute("c_ct_ing"));
			}
			
			//사원관련 검색
			if(coun.getSa_search().equals("sa_nm")){
				if(req.getParameter("sa_find") != null){
					coun.setSa_nm(req.getParameter("sa_find"));
					req.getSession().setAttribute("c_sa_nm", coun.getSa_nm());
				}else{
					coun.setSa_nm((String)req.getSession().getAttribute("c_sa_nm"));
				}
				coun.setSa_no("");
				req.getSession().setAttribute("c_sa_no", "");
			}else if(coun.getSa_search().equals("sa_no")){
				if(req.getParameter("sa_find") != null){
					coun.setSa_no(req.getParameter("sa_find"));
					req.getSession().setAttribute("c_sa_no", coun.getSa_no());
				}else{
					coun.setSa_no((String)req.getSession().getAttribute("c_sa_no"));
				}
				coun.setSa_nm("");
				req.getSession().setAttribute("c_sa_nm", "");
			}
			
			//상품관련 검색
			if(coun.getPd_search().equals("pd_nm")){
				if(req.getParameter("pd_find") != null){
					coun.setPd_nm(req.getParameter("pd_find"));
					req.getSession().setAttribute("c_pd_nm", coun.getPd_nm());
				}else{
					coun.setPd_nm((String)req.getSession().getAttribute("c_pd_nm"));
				}
				coun.setPd_no("");
				req.getSession().setAttribute("c_pd_no", "");
			}else if(coun.getPd_search().equals("pd_no")){
				if(req.getParameter("pd_find") != null){
					coun.setPd_no(req.getParameter("pd_find"));
					req.getSession().setAttribute("c_pd_no", coun.getPd_no());
				}else{
					coun.setPd_no((String)req.getSession().getAttribute("c_pd_no"));
				}
				coun.setPd_nm("");
				req.getSession().setAttribute("c_pd_nm","");
			}
			
			//상담관련 검색
			if(coun.getCs_search().equals("cs_no")){
				if(req.getParameter("cs_find") != null){
					coun.setCs_no(req.getParameter("cs_find"));
					req.getSession().setAttribute("c_cs_no", coun.getCs_no());
				}else{
					coun.setCs_no((String)req.getSession().getAttribute("c_cs_no"));
				}
				coun.setRg_pp("");
				req.getSession().setAttribute("c_rg_pp", "");
			}else if(coun.getCs_search().equals("rg_pp")){
				if(req.getParameter("cs_find") != null){
					coun.setRg_pp(req.getParameter("cs_find"));
					req.getSession().setAttribute("c_rg_pp", coun.getRg_pp());
				}else{
					coun.setRg_pp((String)req.getSession().getAttribute("c_rg_pp"));
				}
				coun.setCs_no("");
				req.getSession().setAttribute("c_cs_no", "");
			}
			
			//상담일관련 검색
			if(req.getParameter("cs_sd") == null || req.getParameter("cs_sd").equals("")){
				if(req.getSession().getAttribute("c_cs_sd") != null && 
						!((String)req.getSession().getAttribute("c_cs_sd")).equals("")){
					coun.setCs_sd((String)req.getSession().getAttribute("c_cs_sd"));
					coun.setCs_ed((String)req.getSession().getAttribute("c_cs_ed"));
				}else{
					coun.setCs_sd("0");
					coun.setCs_ed("0");
					req.getSession().setAttribute("c_cs_sd", "");
					req.getSession().setAttribute("c_cs_ed", "");
				}
			}else{
				coun.setCs_sd(req.getParameter("cs_sd"));
				coun.setCs_ed(req.getParameter("cs_ed"));
				req.getSession().setAttribute("c_cs_sd", coun.getCs_sd());
				req.getSession().setAttribute("c_cs_ed", coun.getCs_ed());
			}
			
			//페이징 관련
			int tot = dao.totalRow();
			int nowPage = 1;
			if(req.getParameter("listPage") == null){
				if(req.getSession().getAttribute("csPage") != null){
					nowPage = (int)req.getSession().getAttribute("csPage");
				}else{
					nowPage = 1;
					req.getSession().setAttribute("csPage", 1);
				}
			}else{
				nowPage = Integer.parseInt(req.getParameter("listPage"));
				req.getSession().setAttribute("csPage", nowPage);
			}
			
			PagingVo paging = new PagingVo();
			paging = paging.computer(tot, nowPage);//페이징 결과값 반환
			
			req.setAttribute("paging", paging);//페이징결과값 저장 저장
			
			//검색용 목록
			ArrayList<CounselDTO> lists = dao.lists(paging.getStartNo(),paging.getEndNo());
			req.setAttribute("lists", lists);

		}else if(full != null){//기본이거나 기본의 페이징일 시
			coun.setFull(full);
			dao.setCoun(coun);
			
			//페이징 관련
			int tot = dao.totalRow();
			int nowPage = 1;
			if(req.getParameter("listPage") == null){
				if(req.getSession().getAttribute("csPage") != null){
					nowPage = (int)req.getSession().getAttribute("csPage");
				}else{
					nowPage = 1;
					req.getSession().setAttribute("csPage", 1);
				}
			}else{
				nowPage = Integer.parseInt(req.getParameter("listPage"));
				req.getSession().setAttribute("csPage", nowPage);
			}
			
			PagingVo paging = new PagingVo();
			paging = paging.computer(tot, nowPage);//페이징 결과값 반환
			
			req.setAttribute("paging", paging);//페이징결과값 저장 저장
			
			ArrayList<CounselDTO> lists = dao.lists(paging.getStartNo(),paging.getEndNo());
			req.setAttribute("lists", lists);
		}
		
		ArrayList<CounselDTO> l = (ArrayList<CounselDTO>)req.getAttribute("lists");
		
		//공통코드 관련
		CodeDTO code = new CodeDTO();
		CodeDAO cdao = new CodeDAO();
		//계약상태
		code.setCd_grp1("CTING");
		cdao.setDto(code);
		ArrayList<CodeDTO> codes = cdao.returnCode();
		req.setAttribute("ctings", codes);
		//직급
		cdao = new CodeDAO();
		code.setCd_grp1("PST");
		cdao.setDto(code);
		codes = cdao.returnCode();
		req.setAttribute("psts", codes);
		//부서
		cdao = new CodeDAO();
		code.setCd_grp1("SADP");
		cdao.setDto(code);
		codes = cdao.returnCode();
		req.setAttribute("sadps", codes);
		
		if(full != null){
			req.getRequestDispatcher("/intranet/counsel/counsel_list_view.jsp").forward(req, resp);
		}else{
			req.getRequestDispatcher("/intranet/index.jsp?inc_content=./counsel/counsel_list.jsp").forward(req, resp);
		}
	}
}
